.Net中 数组、 ArrayList、List、IList数组的区别

在C#中数组,ArrayList,List都能够存储一组对象,之间必有区别

数组Array

  • 类似于线性表,在内存中是连续存储的,便于存储、取值,但一般不做插入、删除操作。
  • 存储相同类型元素的固定大小的顺序集合
  • 使用用一组连续存储单元存放数组的数据元素,存在次序约定问题
  • 大小固定
  • 谨防数组越界
  • 不建议使用数组
    //System.Array

    //1、数组[] 特定类型、固定长度

    string[] str1 = new string[3];

    str1[0] = “a”;

    str1[1] = “b”;

    str1[2] = “c”;

    Console.WriteLine(str1[2]);


    string[] str2 = new string[] { “a”, “b”, “c” };

    Console.WriteLine(str2[0]);


    string[] str3 = { “a”, “b”, “c” };

    Console.WriteLine(str3[0]);

    //2、二维数组

    //int[,] intArray=new int[2,3]{ {1,11,111},{2,22,222}};

    int[,] intArray = new int[2, 3];

    intArray[0, 0] = 1;

    intArray[0, 1] = 11;

    intArray[0, 2] = 111;


    intArray[1, 0] = 2;

    intArray[1, 1] = 22;

    intArray[1, 2] = 222;

    Console.WriteLine(“{0},{1},{2}”, intArray[0, 0], intArray[0, 1], intArray[0, 2]);

    Console.WriteLine(“{0},{1},{2}”, intArray[1, 0], intArray[1, 1], intArray[1, 2]);


    //3、多维数组

    int[, ,] intArray1 = new int[,,]

    {

    {{1, 1}, {11, 11}, {111, 111}},

    {{2, 2}, {22, 22}, {222, 222}},

    {{3, 3}, {33, 33}, {333, 333}}

    };

    Console.WriteLine(“{0},{1},{2},{3},{4},{5}”, intArray1[0, 0, 0], intArray1[0, 0, 1], intArray1[0, 1, 0], intArray1[0, 1, 1],

    intArray1[0, 2, 0], intArray1[0, 2, 1]);

    Console.WriteLine(“{0},{1},{2},{3},{4},{5}”, intArray1[1, 0, 0], intArray1[1, 0, 1], intArray1[1, 1, 0], intArray1[1, 1, 1],

    intArray1[1, 2, 0], intArray1[1, 2, 1]);

    Console.WriteLine(“{0},{1},{2},{3},{4},{5}”, intArray1[2, 0, 0], intArray1[2, 0, 1], intArray1[2, 1, 0], intArray1[2, 1, 1],

    intArray1[2, 2, 0], intArray1[2, 2, 1]);

Array.forEach 函数



Array 对象的每个元素调用指定函数。 此函数是静态的,可在不创建对象实例的情况下调用。 Firefox 除外)中,forEach 函数都忽略数组中值为 undefined 的元素


public static void ForEach<T>(
T[] array,
Action<T> action
)</pre> </div> </div> <div id="syntaxSection" class="section"> <div id="code-snippet-1" class="codeSnippetContainer"> <div class="codeSnippetContainerCodeContainer"> <div id="CodeSnippetContainerCode_1aea524e-38fa-4371-bc09-a48f4c48b531" class="codeSnippetContainerCode" dir="ltr"> <table style="height: 198px;" summary="table" width="364"> <tbody> <tr> <th scope="col"><span id="mt3" class="sentence" data-guid="b4dad0fe5fbef2c0e24d9db1cc69e5a2" data-source="">术语</span></th> <th scope="col"><span id="mt4" class="sentence" data-guid="30618b3b44fa316257d07e387759fae5" data-source="">定义</span></th> </tr> <tr> <td data-th="术语"><span class="parameter">array</span></td> <td data-th="定义"><span id="mt5" class="sentence" data-guid="948352e0d370dd0991aa6f3d5f88330c" data-source="">要枚举的 **Array** 对象。</span></td> </tr> <tr> <td data-th="术语"><span class="parameter">method</span></td> <td data-th="定义"><span id="mt6" class="sentence" data-guid="fba19d57213fe41e88b2d51c372d753c" data-source="">要对数组中每个元素调用的函数。</span></td> </tr> </tbody> </table> <div> <pre>public class SamplesArray
{
public static void Main()
{
// create a three element array of integers
int[] intArray = new int[] {2, 3, 4};

// set a delegate for the ShowSquares method
Action<int> action = new Action<int>(ShowSquares);

Array.ForEach(intArray, action);
}

private static void ShowSquares(int val)
{
Console.WriteLine(“{0:d} squared = {1:d}”, val, valval);
}
}

/

This code produces the following output:

2 squared = 4
3 squared = 9
4 squared = 16
*/`





## ArrayList
  • 在使用该类时必须进行引用(装箱、拆箱)
  • ArrayList对象用来克服Array的缺点
  • 大小是按照其中存储的数据来动态扩充与收缩
  • ArrayList会把所有插入其中的数据当作为object类型来处理
  • 一个对象可存储多种类型的数据
  • 可能会报类型不匹配的错误 不安全
  • 通过调用TrimToSize、设置Capacity属性减少容量
  • 可通过调用 TrimToSize 或通过显式设置 Capacity(容量) 属性减少容量。


    public static void Main() {


    ArrayList myAL = new ArrayList();

    myAL.Add(“Hello”);

    myAL.Add(“World”);

    myAL.Add(“!”);


    Console.WriteLine( “myAL” );

    Console.WriteLine( “ Count: {0}”, myAL.Count );

    Console.WriteLine( “ Capacity: {0}”, myAL.Capacity );

    Console.Write( “ Values:” );

    PrintValues( myAL );

    }


    public static void PrintValues( IEnumerable myList ) {

    foreach ( Object obj in myList )

    Console.Write( “ {0}”, obj );

    Console.WriteLine();

    Console.ReadKey();

    }

    泛型List<T>

  • 解决ArrayList的缺点

  • ArrayList 类的泛型等效类
  • 大小可按需动态增加
  • 必须为其声明List集合内数据的对象类型
  • 可用委托<>


    `(1)声明 List<T>mlist = new List<T>();
    eg: string[] Arr = {“a”,”b”,”c”};

    List&lt;string&gt; mlist = new List&lt;string&gt;(Arr);
    

    (2)添加一个元素 List.Add(T item) 
    eg: mlist.Add(“d”);
    (3)添加集合元素
    eg: string[] Arr2 ={“f”,”g”.”h”};

    mlist.AddRange(Arr2);
    

    (4)在index位置添加一个元素 Insert(int index,T item)
    eg: mlist.Insert(1,”p”);
    (5)遍历List中元素
      foreach(T element in mlist) T的类型与mlist声明时一样
         {
           Console.WriteLine(element);

          }
    eg:
    foreach(string s in mlist)
          {
             Console.WriteLine(s);
           }
    

    (6)删除元素

    List.Remove(T item) 删除一个值
    eg: mlist.Remove("a");
    List.RemoveAt(int index);删除下标为index的元素
    eg: mlist.RemoveAt(0);
    
    List.RemoveRange(int index,int count); 下标index开始,删除count个元素
    eg:mlist.RemoveRange(3,2);
    

    (7)判断某个元素是否在该List中

    List.Contains(T item) 返回true或false
    eg:
    if(mlist.Contains"("g"))
       Console.WriteLine("g存在列表中");
    else
       mlist.Add("g");
    

    (8)给List里面元素排序 List.Sort() 默认是元素每一个字母按升序
    eg: mlist.Sort();
    (9)给List里面元素顺序反转 List.Reverse() 可以与List.Sort()配合使用
    (10)List清空 List.Clear()
    eg: mlist.Clear();
    (11)获得List中元素数目 List.Count() 返回int值
    eg: mlist.count();


 

总结

http://blog.csdn.net/zhangxinbin5/article/details/8987277

blog.jobbole.com/102452

https://msdn.microsoft.com/en-us/library/zecdkyw2(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

 

文章目录
  1. 1. 数组Array
    1. 1.1. Array.forEach 函数
  2. 2. 泛型List
  3. 3. 总结
|